最近在寫多模組的專案,也順便讀了 Effective Kotlin ,不過我決定先分享一下,使用 Gradle 從 Groovy 到 Kotlin-dsl 的小心得
基本上非常簡單,圍著幾個概念走就行
最最基本的遷移大概長這樣,那一個基礎專案的設置會變得大概
差點忘了說,build.gradle -> build.gradle.kts
plugins {
id("com.android.application")
kotlin("android")
kotlin("kapt")
id("dagger.hilt.android.plugin")
}
android {
compileSdk = ProjectConfig.compileSdk
defaultConfig {
applicationId = ProjectConfig.appId
minSdk = ProjectConfig.minSdk
targetSdk = ProjectConfig.targetSdk
versionCode = ProjectConfig.versionCode
versionName = ProjectConfig.versionName
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
useSupportLibrary = true
}
}
buildTypes {
getByName("release"){
isMinifyEnabled = false
proguardFiles(getDefaultProguardFile("proguard-android.txt"), "proguard-rules.pro")
}
}
compileOptions {
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
}
kotlinOptions {
jvmTarget = "1.8"
}
composeOptions {
kotlinCompilerExtensionVersion = Compose.composeCompilerVersion
}
buildFeatures {
compose = true
}
}
kapt {
correctErrorTypes = true
}
dependencies {
implementation(Compose.compiler)
implementation(Compose.ui)
implementation(Compose.uiToolingPreview)
implementation(Compose.hiltNavigationCompose)
implementation(Compose.material)
implementation(Compose.runtime)
implementation(Compose.navigation)
implementation(Compose.viewModelCompose)
implementation(Compose.activityCompose)
implementation(DaggerHilt.hiltAndroid)
kapt(DaggerHilt.daggerHiltCompiler)
implementation(project(Modules.core))
testImplementation(Testing.junit4)
testImplementation(Testing.junitAndroidExt)
testImplementation(Testing.truth)
testImplementation(Testing.coroutines)
testImplementation(Testing.turbine)
testImplementation(Testing.composeUiTest)
testImplementation(Testing.mockk)
testImplementation(Testing.mockWebServer)
androidTestImplementation(Testing.junit4)
androidTestImplementation(Testing.junitAndroidExt)
androidTestImplementation(Testing.truth)
androidTestImplementation(Testing.coroutines)
androidTestImplementation(Testing.turbine)
androidTestImplementation(Testing.composeUiTest)
androidTestImplementation(Testing.mockkAndroid)
androidTestImplementation(Testing.mockWebServer)
androidTestImplementation(Testing.hiltTesting)
kaptAndroidTest(DaggerHilt.daggerHiltCompiler)
androidTestImplementation(Testing.testRunner)
}
現在裡面那些 Compose.compiler
被我抓到 buildSrc 管理了,之後再說,如果不知道的就用implementation("androidx.compose.compiler:compiler:$compose_version")
其實一樣啦
剛遷移的時候,不知道怎麼下關鍵字,一直查不到 maven 要怎麼改,後來爬別人的 source code 才看到寫法
//before
repositories {
mavenCentral()
maven { "https://kotlin.bintray.com/ktor" }
//after
maven ( url = "https://kotlin.bintray.com/ktor" )
之後補充:)